//
//  ACESelectPickerView.m
//  ACE
//
//  Created by zhangtao on 2018/8/9.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "ACESelectPickerView.h"

@implementation ACESelectPickerView
{
    NSString *value;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self createPicker];
    }
    return self;
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    if (_myPickerView) {
//        [_myPickerView reloadAllComponents];
        [_myPickerView reloadData];
    }else{
        [self createPicker];
    }
}

-(void)createPicker{
    _myPickerView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStyleGrouped];
    _myPickerView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myPickerView.delegate = self;
    _myPickerView.dataSource = self;
    _myPickerView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_myPickerView];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return .1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return .1;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]init];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ACESelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[ACESelectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.title.text = _dataArray[indexPath.row];
    if (_sourceValue && [_sourceValue isEqualToString:_dataArray[indexPath.row]]) {
        cell.title.textColor = deepGreen;
    }else{
        cell.title.textColor = [UIColor darkGrayColor];
    }
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ACESelectCell *cell = [_myPickerView cellForRowAtIndexPath:indexPath];
    NSString *str = cell.title.text;
    if (self.SelectPickerCallBack) {
        self.SelectPickerCallBack(str);
    }
}

//////////////////////////

/*

-(void)createPicker{
    _myPickerView = [[UIPickerView alloc]initWithFrame:self.bounds];
    _myPickerView.delegate = self;
    
    [self addSubview:_myPickerView];
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    if (_myPickerView) {
        [_myPickerView reloadAllComponents];
    }else{
        [self createPicker];
    }
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _dataArray.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return _dataArray[row];
}



-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 45;
}


-(void)click{
    NSLog(@"...");
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel *lab=(UILabel *)view;
    if (!lab) {
        lab=[[UILabel alloc] init];
        lab.userInteractionEnabled = YES;
        lab.minimumScaleFactor=8;//设置最小字体，与minimumFontSize相同，minimumFontSize在IOS 6后不能使用。
        lab.adjustsFontSizeToFitWidth=YES;//设置字体大小是否适应lalbel宽度
        lab.textAlignment=NSTextAlignmentCenter;//文字居中显示
        [lab setTextColor:[UIColor blackColor]];
        if (row == 0) {
            lab.textColor = deepGreen;
        }
        [lab setFont:[UIFont systemFontOfSize:17.0f]];
        UIView *bottom = [[UIView alloc]initWithFrame:CGRectMake(50, 44, ACEWidth-100,1)];
        bottom.backgroundColor = lightGreen;
        [lab addSubview:bottom];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [lab addGestureRecognizer:tap];
    }
    lab.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return lab;
    
    
    
//    UILabel *lab  = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ACEWidth, 45)];
//    
//    lab.textAlignment = NSTextAlignmentCenter;
//    lab.text = _dataArray[row];
//    UIView *bottom = [[UIView alloc]initWithFrame:CGRectMake(50, 44, ACEWidth-100,1)];
//    bottom.backgroundColor = lightGreen;
//    
//    [lab addSubview:bottom];
//    return view;
}



-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    UILabel *lab = (UILabel *)[pickerView viewForRow:row forComponent:component];
    lab.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [lab addGestureRecognizer:tap];
    lab.textColor = deepGreen;

    
    
//    for (UIView *subView in view.subviews) {
//        if ([subView isKindOfClass:[UILabel class]]) {
//            UILabel *lab = (UILabel *)subView;
//            lab.textColor = deepGreen;
//
//        }
//    }
    
//    value = _dataArray[row];
//    if (self.SelectPickerCallBack) {
//        self.SelectPickerCallBack(value);
//    }
}



-(void)tap:(UITapGestureRecognizer *)tap{
    UILabel *lab = (UILabel *)tap.view;
    NSString *str = lab.text;
    value =str;
        if (self.SelectPickerCallBack) {
            self.SelectPickerCallBack(value);
        }
}
*/

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

@implementation ACESelectCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _title=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ACEWidth, 45)];
        _title.minimumScaleFactor=8;//设置最小字体，与minimumFontSize相同，minimumFontSize在IOS 6后不能使用。
        _title.adjustsFontSizeToFitWidth=YES;//设置字体大小是否适应lalbel宽度
        _title.textAlignment=NSTextAlignmentCenter;//文字居中显示
        [_title setTextColor:[UIColor blackColor]];
       
        [_title setFont:[UIFont systemFontOfSize:17.0f]];
        UIView *bottom = [[UIView alloc]initWithFrame:CGRectMake(50, 44, ACEWidth-100,1)];
        bottom.backgroundColor = lightGreen;
        [_title addSubview:bottom];
        [self addSubview:_title];
    }
    return self;
}

@end
